package hk.controller;

import hk.beans.Account;
import hk.beans.Payment;
import hk.common.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/4/24 14:45
 */
@RestController
@RequestMapping("/order")
public class BankController {
    //支付服务的地址
    private static final String PAYMENT_URL="http://HK-PAYMENT-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/getpayment/{id}")
    public R<?> getOrderPayment(@PathVariable("id") int id){
        return restTemplate.getForObject(
                PAYMENT_URL+"/payment/getpayment/"+id,
                R.class
        );
    }

    @PostMapping("/addpayment")
    public R<?> addOrder(Payment payment){
        //使用  restTemplate 进行远程调用
        R<?> r=restTemplate.postForObject(
                PAYMENT_URL+"/payment/addpayment",
                payment,
                R.class
        );

        System.out.println("下单成功! 支付成功!");
        return r;
    }

    @PostMapping("/addaccount")
    public R<?> addAccount(Account account, HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        //使用  restTemplate 进行远程调用
        R<?> r=restTemplate.postForObject(
                PAYMENT_URL+"/account/addaccount",
                account,
                R.class
        );

        System.out.println("下单成功! 支付成功!");
        return r;
    }

    @GetMapping("/getaccount/{account}")
    public R<?> getOrderAccount(@PathVariable("account") String accountId){
        return restTemplate.getForObject(
                PAYMENT_URL+"/account/getaccount/"+accountId,
                R.class
        );
    }

    @GetMapping("/getaccount")
    public R<?> getAllMsg(){
        return restTemplate.getForObject(
                PAYMENT_URL+"/account/getaccount",
                R.class
        );
    }

    @GetMapping("/getallmoney")
    public R<?> getAllMoney(){
        return restTemplate.getForObject(
                PAYMENT_URL+"/account/getallmoney",
                R.class
        );
    }

    @GetMapping("/delete/{id}")
    public R<?> delete(@PathVariable("id") Integer id){
        return restTemplate.getForObject(
                PAYMENT_URL+"/account/delete/"+id,
                R.class
        );
    }
}
